<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺状态" prop="shopState">
        <el-select v-model="queryParams.shopState" placeholder="请选择店铺状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_shop_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="平台" prop="platformName">
        <el-select v-model="queryParams.platformName" placeholder="请选择平台" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_platform_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺类型" prop="shopType">
        <el-select v-model="queryParams.shopType" placeholder="请选择店铺类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_shop_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主体名" prop="shopPrid">
        <el-input
          v-model="queryParams.shopPrid"
          placeholder="请输入主体名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="shopUid">
        <el-input
          v-model="queryParams.shopUid"
          placeholder="请输入负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="团队" prop="shopTeam">
        <el-input
          v-model="queryParams.shopTeam"
          placeholder="请输入团队"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="shopPid">
        <el-input
          v-model="queryParams.shopPid"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号使用人" prop="phoneName">
        <el-input
          v-model="queryParams.phoneName"
          placeholder="请输入手机号使用人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['item:shop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['item:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['item:shop:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['item:shop:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺id" align="center" prop="shopId" />
      <el-table-column label="店铺名" align="center" prop="shopName" />
      <el-table-column label="店铺状态" align="center" prop="shopState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_shop_status" :value="scope.row.shopState"/>
        </template>
      </el-table-column>
      <el-table-column label="平台" align="center" prop="platformName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_platform_type" :value="scope.row.platformName"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺类型" align="center" prop="shopType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_shop_type" :value="scope.row.shopType"/>
        </template>
      </el-table-column>
      <el-table-column label="主体名" align="center" prop="companyInfo.companyName" />
      <el-table-column label="部门" align="center" prop="companyId" />
      <el-table-column label="负责人" align="center" prop="user.userName" />
      <el-table-column label="团队" align="center" prop="shopTeam" />
      <el-table-column label="手机号" align="center" prop="phone.phoneNumber" />
      <el-table-column label="账号" align="center" prop="shopAccountname" />
      <el-table-column label="密码" align="center" prop="shopPassword" />
      <el-table-column label="绑定邮箱" align="center" prop="emailAdress" />
      <template>
        <el-button type="text" @click="open">卡号</el-button>
      </template>

      <el-table-column label="手机号使用人" align="center" prop="user1.userName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['item:shop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['item:shop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名" />
        </el-form-item>
        <el-form-item label="店铺状态" prop="shopState">
          <el-select v-model="form.shopState" placeholder="请选择店铺状态">
            <el-option
              v-for="dict in dict.type.sys_shop_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="平台" prop="platformName">
          <el-select v-model="form.platformName" placeholder="请选择平台">
            <el-option
              v-for="dict in dict.type.sys_platform_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺类型" prop="shopType">
          <el-select v-model="form.shopType" placeholder="请选择店铺类型">
            <el-option
              v-for="dict in dict.type.sys_shop_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主体名" prop="companyInfo.companyName">
          <el-input v-model="form.shopPrid" placeholder="请输入主体名" />
        </el-form-item>
        <el-form-item label="部门" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="负责人" prop="user.userName">
          <el-input v-model="form.shopUid" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="团队" prop="shopTeam">
          <el-input v-model="form.shopTeam" placeholder="请输入团队" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone.phoneNumber">
          <el-input v-model="form.shopPid" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="账号" prop="shopAccountname">
          <el-input v-model="form.shopAccountname" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="shopPassword">
          <el-input v-model="form.shopPassword" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="绑定邮箱" prop="emailAdress">
          <el-input v-model="form.emailAdress" placeholder="请输入绑定邮箱" />
        </el-form-item>
        <el-form-item label="绑定卡号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入绑定卡号" />
        </el-form-item>
        <el-form-item label="手机号使用人" prop="user1.userName">
          <el-input v-model="form.phoneName" placeholder="请输入手机号使用人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="yun" :visible.sync="open" width="500px" append-to-body>
        <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="" align="center" prop="accountId" />
          <el-table-column label="账户平台" align="center" prop="accountPlatform">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_account_platform" :value="scope.row.accountPlatform"/>
            </template>
          </el-table-column>
          <el-table-column label="账户类型" align="center" prop="accoutType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_accout_type" :value="scope.row.accoutType"/>
            </template>
          </el-table-column>
          <el-table-column label="账户名" align="center" prop="accountName" />
          <el-table-column label="账号" align="center" prop="accountNumber" />
        </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop } from "@/api/item/shop";
import { listAccount } from "@/api/system/account";
export default {
  name: "Shop",
  dicts: ['sys_platform_type', 'sys_shop_type', 'sys_shop_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺管理表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopName: null,
        shopState: null,
        platformName: null,
        shopType: null,
        shopPrid: null,
        companyId: null,
        shopUid: null,
        shopTeam: null,
        shopPid: null,
        phoneName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    open() {},
    /** 查询店铺管理列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shopId: null,
        shopName: null,
        shopState: null,
        platformName: null,
        shopType: null,
        shopPrid: null,
        companyId: null,
        shopUid: null,
        shopTeam: null,
        shopPid: null,
        shopAccountname: null,
        shopPassword: null,
        emailAdress: null,
        bankAccount: null,
        phoneName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.shopId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shopId = row.shopId || this.ids
      getShop(shopId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shopId != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shopIds = row.shopId || this.ids;
      this.$modal.confirm('是否确认删除店铺管理编号为"' + shopIds + '"的数据项？').then(function() {
        return delShop(shopIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('item/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
